package com.tianhui.consignortwofei.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.AutoCompleteTextView;

import com.fgs.common.CommonFragment;
import com.fgs.common.utils.LogUtil;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseDataListActivity;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.contract.SourcePathContract;
import com.tianhui.consignortwofei.mvp.model.enty.SourcePathInfo;
import com.tianhui.consignortwofei.mvp.presenter.SourcePathPresenter;
import com.tianhui.consignortwofei.mvp.ui.adapter.SourcePathListAdapter;
import com.xuexiang.xutil.net.JsonUtil;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.inter.OnItemClickListener;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/8/31
 * @description
 **/
public class SourcePathListActivity extends BaseDataListActivity<SourcePathContract.View, SourcePathContract.Presenter> implements SourcePathContract.View {

    @BindView(R.id.layout_search_editText)
    AutoCompleteTextView searchKeyEditText;

    private int mType;
    private SourcePathListAdapter mPathListAdapter;
    private SourcePathInfo mPathInfo;
    private String mSearchKey;

    @Override
    public int getLayoutId() {
        return R.layout.activity_source_path_list;
    }

    @Override
    public SourcePathContract.Presenter createPresenter() {
        return new SourcePathPresenter(this);
    }

    @Override
    public SourcePathContract.View createView() {
        return this;
    }


    @Override
    public String getToolbarTitle() {
        return "历史信息";
    }


    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @Override
    public RecyclerArrayAdapter createAdapter() {
        mPathListAdapter = new SourcePathListAdapter(this);
        return mPathListAdapter;
    }

    @Override
    protected void initListener() {

        mPathListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                mPathListAdapter.setSelectPosition(position);
                mPathListAdapter.notifyDataSetChanged();
                SourcePathInfo pathInfo = mPathListAdapter.getAllData().get(position);
                setSelectPathInfo(pathInfo);
            }
        });

    }

    private void setSelectPathInfo(SourcePathInfo pathInfo) {
        if (pathInfo == null) return;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(pathInfo.placeprovince);
        if (!TextUtils.isEmpty(pathInfo.placecity)) {
            stringBuilder.append(" ");
            stringBuilder.append(pathInfo.placecity);
        }
        if (!TextUtils.isEmpty(pathInfo.placecounty)) {
            stringBuilder.append(" ");
            stringBuilder.append(pathInfo.placecounty);
        }
        mPathInfo = pathInfo;
        mPathInfo.address = stringBuilder.toString();


    }

    @OnClick(R.id.activity_source_path_list_confirmButton)
    public void confirm() {

        int size = mAdapter.getCount();
        if (size > 0) {
            SourcePathInfo sourcePathInfo = mPathListAdapter.getSelectPathInfo();
            sourcePathInfo.placeparentcode = sourcePathInfo.placecitycode;
            setSelectPathInfo(sourcePathInfo);
            Intent intent = new Intent();
            intent.putExtra(KeyValue.KEY_PATH_INFO, mPathInfo);
            setResult(mType, intent);
            finish();
            LogUtil.print("SourcePathListActivity 信息", JsonUtil.toJson(mPathInfo));
        } else {
            showToast("暂无可导入信息");
        }


    }

//    @Override
//    public void showDataList(List data) {
//        super.showDataList(data);
//        if (data != null && data.size() > 0) {
//            setSelectPathInfo(mPathListAdapter.getAllData().get(0));
//        }
//    }

    @Override
    public void requestDataList(int page, boolean isDialog) {

        if (TextUtils.isEmpty(mSearchKey)) {
            mSearchKey = "";
        }
        Map<String, String> map = new HashMap<>();
        map.put("limit", "10");
        map.put("offset", "10");
        map.put("page", page + "");
        map.put("type", mType + "");
        map.put("name", mSearchKey);
        getPresenter().requestSourceList(map, isDialog);
    }

    @Override
    public void initData() {

        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            mType = bundle.getInt(KeyValue.KEY_TYPE);
        }
        mPathInfo = new SourcePathInfo();

        searchKeyEditText.setHint("请输入联系人");

    }

    @OnClick(R.id.layout_search_textView)
    public void search() {
        mSearchKey = searchKeyEditText.getText().toString();

        setRefresh(true);
        requestDataList(1, true);
    }
}
